/*     */ package com.java.diagnostics.visualizer.impl.strings;
/*     */ 
/*     */ import java.util.Vector;
/*     */ 
/*     */ public class TextReplacer
/*     */ {
/*     */   private ReplacerNode root;
/*     */   public static final int HIGHEST_REPLACE_CHAR = 255;
/*     */   private static final byte NO_MATCH = 0;
/*     */   private static final byte PARTIAL_MATCH = 1;
/*  26 */   private Vector matchTable = new Vector();
/*     */ 
/*     */   public TextReplacer(StringPair[] list)
/*     */   {
/*  32 */     if (list.length >= 128) {
/*  33 */       throw new Error("can only replace 128 sequences at a time");
/*     */     }
/*     */ 
/*  36 */     this.root = new ReplacerNode(255);
/*  37 */     for (int i = 0; i < list.length; i++)
/*  38 */       growTable(list[i]);
/*     */   }
/*     */ 
/*     */   private void growTable(StringPair aPair)
/*     */   {
/*  43 */     int keyLen = aPair.key.length();
/*  44 */     ReplacerNode cursor = this.root;
/*  45 */     for (int i = 0; i < keyLen; i++) {
/*  46 */       char charVal = aPair.key.charAt(i);
/*  47 */       if (charVal > 'ÿ') {
/*  48 */         throw new Error("cannot replace values greater than 255");
/*     */       }
/*     */ 
/*  51 */       ReplacerNode next = cursor.nextNode[charVal];
/*  52 */       if (next != null) {
/*  53 */         cursor = next;
/*     */       }
/*  55 */       else if (i == keyLen - 1) {
/*  56 */         this.matchTable.addElement(aPair.value);
/*     */ 
/*  59 */         cursor.action[charVal] = ((byte)(this.matchTable.size() + 1));
/*     */       } else {
/*  61 */         cursor.nextNode[charVal] = new ReplacerNode(
/*  62 */           255);
/*  63 */         cursor.action[charVal] = 1;
/*  64 */         cursor = cursor.nextNode[charVal];
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public String replaceText(String input)
/*     */   {
/*  71 */     int inLength = input.length();
/*  72 */     StringBuffer output = new StringBuffer(inLength);
/*  73 */     ReplacerNode current = this.root;
/*  74 */     int offset = -1;
/*     */ 
/*  76 */     for (int i = 0; i < inLength; i++) {
/*  77 */       char currentChar = input.charAt(i);
/*     */ 
/*  79 */       if ((currentChar >= ' ') && (currentChar <= 'z')) {
/*  80 */         byte action = currentChar <= 'ÿ' ? current.action[currentChar] : 
/*  81 */           0;
/*  82 */         switch (action) {
/*     */         case 0:
/*  84 */           if (offset == -1) {
/*  85 */             output.append(currentChar);
/*     */           }
/*     */           else {
/*  88 */             output.append(input.charAt(offset));
/*  89 */             current = this.root;
/*  90 */             i = offset;
/*     */ 
/*  92 */             offset = -1;
/*     */           }
/*  94 */           break;
/*     */         case 1:
/*  96 */           if (offset == -1) {
/*  97 */             offset = i;
/*     */           }
/*  99 */           current = current.nextNode[currentChar];
/* 100 */           break;
/*     */         default:
/* 102 */           output.append(this.matchTable.elementAt(action - 2));
/* 103 */           offset = -1;
/* 104 */           current = this.root;
/*     */         }
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 110 */     return output.toString();
/*     */   }
/*     */ }

